package com.xbongbong.lfasr.concurrent;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.redis.RedisCacheAutoConfiguration;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author: wujian
 * @create: 2021-12-08 09:57
 * @description: 防止超发出库
 **/
@Component
public class ProductRedisLock {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductRedisLock.class);

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 超时时间 30分钟
     */
    public static final int TIMEOUT = 30;

    @Resource
    private RedisCacheAutoConfiguration redisCacheAutoConfiguration;

    /**
     * 判断是否有相同的数据
     * @param prefix  前缀
     * @param lockKey 锁key
     * @param productIdList key 对应的值
     *
     */
    public Boolean productCostsLock(String prefix, String lockKey, List<String> productIdList) {
        List<String> keys = Arrays.asList(ProBaseConfig.getProjectName() + ":" + prefix + ":" + lockKey);
        String pr = JSON.toJSONString(productIdList);
        Boolean luaExistKeyDel = stringRedisTemplate.execute(redisCacheAutoConfiguration.redisProductCostsLock(), keys, JSON.toJSONString(productIdList));
        Set<String> set = stringRedisTemplate.opsForSet().members(ProBaseConfig.getProjectName() + ":" + prefix + ":" + lockKey);
        LOGGER.error("set:"+set);
        if( luaExistKeyDel) {
            LOGGER.info("可以执行");
        } else {
            // TODO
            LOGGER.error("不可以执行!");
        }
        return luaExistKeyDel;
    }


    /**
     * 对同一件商品解锁
     * @param prefix  前缀
     * @param lockKey 锁key
     * @param productIdList key 对应的值
     *
     */
    public void productCostsUnLock(String prefix, String lockKey, List<String> productIdList) {
        List<String> keys = Arrays.asList(ProBaseConfig.getProjectName() + ":" + prefix + ":" + lockKey);
        Boolean luaExistKeyDel = stringRedisTemplate.execute(redisCacheAutoConfiguration.redisProductCostsUnLock(), keys, JSON.toJSONString(productIdList));
        Set<String> set = stringRedisTemplate.opsForSet().members(ProBaseConfig.getProjectName() + ":" + prefix + ":" + lockKey);
        LOGGER.error("set:"+set);
        if( luaExistKeyDel) {
            LOGGER.info("可以执行");
        } else {
            // TODO
            LOGGER.error("不可以执行!");
        }
    }


    /**
     * @Author: wujian
     * @Description: 将需要添加的redis中的产品数据插入redis  且对比此次出库入数量是否超发
     * @Date: 上午10:35 2021/12/8
     * @Param: [prefix, lockKey, needAddMap需要将库存新加到redis的数据, needContrastMap本次出入库的数据,keyList本次添加的所有产品的小key]
     * @return: boolean
     **/
    public boolean handleProductStock(String prefix, String lockKey, List<String> needAddMap){
        List<String> keys = Arrays.asList(ProBaseConfig.getProjectName() + ":" + prefix + ":" + lockKey);

        Boolean isSuccess = stringRedisTemplate.execute(redisCacheAutoConfiguration.redisProductStockLock(), keys, JSON.toJSONString(needAddMap));
        if( isSuccess) {
            LOGGER.info("可以执行");
        } else {
            // TODO
            LOGGER.error("不可以执行!");
        }
        return isSuccess;
    }

    /**
     * @Author: wujian
     * @Description: 将需要添加的redis中的产品数据插入redis  且对比此次出库入数量是否超发
     * @Date: 上午10:35 2021/12/8
     * @Param: [prefix, lockKey, needAddMap需要将库存新加到redis的数据, needContrastMap本次出入库的数据,keyList本次添加的所有产品的小key]
     * @return: boolean
     **/
    public boolean handleSingleProductStock(String prefix, List<String> redisKeyList,List<String> needAddMap){
        List<String> keys = new ArrayList<>();
        keys.add(ProBaseConfig.getProjectName() + ":" + prefix + ":");
//        if (CollectionsUtil.isNotEmpty(redisKeyList)){
//            for (String key : redisKeyList) {
//                String redisKey =  ProBaseConfig.getProjectName() + ":" + "{" + prefix + "}" + ":"  + key ;
//                keys.add(redisKey);
//            }
//        }
        List<String> list = new ArrayList();
        if (CollectionsUtil.isNotEmpty(needAddMap)){
            for (String str : needAddMap) {
                String s =  ProBaseConfig.getProjectName() + ":" + "{" + prefix + "}" + ":" + str;
                list.add(s);
            }
        }
        String [] needAddArray = list.toArray(new String[]{});
        Boolean isSuccess = stringRedisTemplate.execute(redisCacheAutoConfiguration.redisSingleProductStockLock(), keys, needAddArray);
        if( isSuccess) {
            LOGGER.info("可以执行");
        } else {
            // TODO
            LOGGER.error("不可以执行!");
        }
        return isSuccess;
    }


}
